<?php
// Global function for accepting the path to an asset and fetching it

function glueprint($item) {
#	require ("glueprint.conf");
	$filename = "glueprint/$item.php";
	if (file_exists($filename)) { 
       $error_reporting = error_reporting(E_ERROR);
       include($filename);
       error_reporting($error_reporting);
	} else { 
		print "<em style=\"background-color:#efd\"><span style=\"color:#693\">Glueprint couldn't locate \"glueprint/$item.php\". Please double-check that the path and filename are valid.</span></em>";
	}
}

// Default functions for connecting directly with your library of assets (strings, images, etc)
// These are tiny helper functions (a.k.a "glue code") that simplify the include process by linking
// directly to the appropriate sub-directory of "glueprint/assets".

function glue_string($item) { 
	glueprint("assets/strings/$item.string"); 
}


// This last function accepts a path to any subdirectoy of "glueprint/assets".
// Use it to quickly test the retrival of a new asset type.

function glue($item) { 
	glueprint("assets/$item"); 
}


function glue_ad_bail($text) {
	
	echo "<em style=\"background-color:#efd\"><span style=\"color:#693\">";
    echo "Glue Ad Problem: ";
    echo "<strong>".$text."</strong>";
	echo "</span></em>";
}

function glue_ad($type) {

    // sanitize
    if (!ctype_alnum($type)) {
        glue_ad_bail("$type is not valid");
        return;
    }

    // find the directory
    $path = "glueprint/assets/ads/$type/";
    if (!is_dir($path) || !is_readable($path)) {
        glue_ad_bail("No assets could be located @ $path");
        return;
    }

    // select a random item in the directory
    $dh = opendir($path);
    $possiblefiles = array();
    while (($file = readdir($dh)) !== false) {
        if (is_readable($path . $file)
            && !is_dir($path . $file)
            && $file{0} != '.'
            && strtolower(substr($file, -4)) != '.php'
            ) {
            $possiblefiles[] = $file;
        }
    }
    $key = array_rand($possiblefiles);
    $selectedfile = $path . $possiblefiles[$key];

    // locate the .php containing the gluecode
    if (strtolower(substr($selectedfile, -4)) == '.swf') {
        $adcodefile = "glueprint/assets/ads/flash.ad.php";
        if (file_exists($path . 'flash.ad.php')) {
            $adcodefile = $path . 'flash.ad.php';
        }
    } else {
        $adcodefile = "glueprint/assets/ads/image.ad.php";
        if (file_exists($path . 'image.ad.php')) {
            $adcodefile = $path . 'image.ad.php';
        }
    }

    if (!is_readable($adcodefile)) {
        glue_ad_bail("$adcodefile is not readable");
        return;
    }

    // prepare variables for the ad.php
    
    $parts = split("x", $type);
    if (count($parts) != 2 || !is_numeric($parts[0]) || !is_numeric($parts[1])) {
        glue_ad_bail("$type is not height x width");
        return;
    }
    $ad = $selectedfile;
    $width = $parts[0];
    $height = $parts[1];

    include($adcodefile);

}

;?>
